package com.ewanghuiju.app.presenter.my;

import android.widget.Toast;

import com.ewanghuiju.app.R;
import com.ewanghuiju.app.app.App;
import com.ewanghuiju.app.base.RxPresenter;
import com.ewanghuiju.app.base.contract.my.ChangeBodyWithCodeContract;
import com.ewanghuiju.app.model.DataManager;
import com.ewanghuiju.app.model.bean.request.PhoneRegistbindRequestBean;
import com.ewanghuiju.app.model.bean.response.PhoneCodeResponseBean;
import com.ewanghuiju.app.model.bean.response.PhoneRegistbindResponBean;
import com.ewanghuiju.app.model.http.response.BaseResponse;
import com.ewanghuiju.app.model.http.response.MyHttpResponse;
import com.ewanghuiju.app.model.http.response.Optional;
import com.ewanghuiju.app.util.RxUtil;
import com.ewanghuiju.app.widget.CommonSubscriber;
import com.muddzdev.styleabletoast.StyleableToast;

import java.util.concurrent.TimeUnit;

import javax.inject.Inject;

import io.reactivex.Flowable;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;

/**
 * @author sj  qq:751943509
 * @class 修改信息  用到手机验证码页面
 * @time 2020/2/14
 */
public class ChangeBodyWithCodePresenter extends RxPresenter<ChangeBodyWithCodeContract.View> implements ChangeBodyWithCodeContract.Presenter {
    private DataManager mDataManager;

    private Disposable intervalSubscription;

    private static final int INTERVAL_INSTANCE = 2;

    @Inject
    public ChangeBodyWithCodePresenter(DataManager mDataManager) {
        this.mDataManager = mDataManager;
    }

    @Override
    public void editpwd(PhoneRegistbindRequestBean phoneRegistbindRequestBean) {
        //修改密码
        addSubscribe(mDataManager.userEditpwd(phoneRegistbindRequestBean)
                .compose(RxUtil.<MyHttpResponse<PhoneRegistbindResponBean>>rxSchedulerHelper())
                .compose(RxUtil.handleMyResultCannull())
                .subscribeWith(new CommonSubscriber<Optional<PhoneRegistbindResponBean>>(mView) {
                    @Override
                    public void onNext(Optional<PhoneRegistbindResponBean> phoneRegistbindResponBean) {
                        mView.editpwdSuccess();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                })
        );
    }

    @Override
    public void mobileEdit(String old_mobile, String pwd, String mobile, String code) {
        //修改手机号
        addSubscribe(mDataManager.mobileEdit(old_mobile, pwd, mobile, code)
                .compose(RxUtil.<MyHttpResponse<BaseResponse>>rxSchedulerHelper())
                .compose(RxUtil.<BaseResponse>handleMyResultCannull())
                .subscribeWith(new CommonSubscriber<Optional<BaseResponse>>(mView) {
                    @Override
                    public void onNext(Optional<BaseResponse> baseResponse) {
                        if (mView != null)
                            mView.mobileEditSuccess();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                })
        );
    }

    @Override
    public void setPayPassword(String password) {
        //设置支付密码
        addSubscribe(mDataManager.setPaypwd(password, password)
                        .compose(RxUtil.<MyHttpResponse<BaseResponse>>rxSchedulerHelper())
                        .compose(RxUtil.<BaseResponse>handleMyResultCannull())
                        .subscribeWith(new CommonSubscriber<Optional<BaseResponse>>(mView) {
                            @Override
                            public void onNext(Optional<BaseResponse> baseResponse) {
                                if (mView != null)
                                    mView.showPayPassword();
                            }

                            @Override
                            public void onError(Throwable e) {
                                super.onError(e);
//                        if (e instanceof ApiException) {
//                            if (mView != null)
//                                mView.showPayPasswordError(((ApiException) e).getCode(), e.getMessage());
//                        }
                            }
                        })
        );
    }


    @Override
    public void bindAlipay(String uname, String alipay, String mobile, String code) {
        //绑定支付宝
        addSubscribe(mDataManager.updateAlipay(uname, alipay, mobile, code)
                .compose(RxUtil.<MyHttpResponse<BaseResponse>>rxSchedulerHelper())
                .compose(RxUtil.<BaseResponse>handleMyResultCannull())
                .subscribeWith(new CommonSubscriber<Optional<BaseResponse>>(mView) {
                    @Override
                    public void onNext(Optional<BaseResponse> baseResponse) {
                        if (mView != null)
                            mView.bindAlipaySuccess();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                })
        );
    }

    @Override
    public void getCode(String mobile) {
        //获取验证码
        addSubscribe(mDataManager.getCode(mobile)
                .compose(RxUtil.<MyHttpResponse<PhoneCodeResponseBean>>rxSchedulerHelper())
                .compose(RxUtil.handleMyResultCannull())
                .subscribeWith(new CommonSubscriber<Optional<PhoneCodeResponseBean>>(mView) {
                    @Override
                    public void onNext(Optional<PhoneCodeResponseBean> String) {
                        StyleableToast.makeText(App.getInstance(), "获取验证码成功", Toast.LENGTH_SHORT, R.style.mytoast).show();
                        startInterval();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                })
        );
    }

    @Override
    public void startInterval() {
        final int count = 60;

        if (intervalSubscription != null && !intervalSubscription.isDisposed()) {
            return;
        }
        intervalSubscription = Flowable.interval(0, 1, TimeUnit.SECONDS)
                .take(count + 1)//设置循环次数
                .onBackpressureDrop()
                .compose(RxUtil.<Long>rxSchedulerHelper())
                .map(new Function<Long, Long>() {
                    @Override
                    public Long apply(Long aLong) throws Exception {
                        return count - aLong;
                    }
                }).subscribe(new Consumer<Long>() {
                    @Override
                    public void accept(Long aLong) throws Exception {
                        if (mView == null)
                            return;
                        if (aLong >= count) {
                            mView.showCodeOn();
                            mView.showCodeText("重新获取(" + aLong + "s)");
                        } else if (aLong <= 0) {
                            mView.showCodeComplete();
                        } else {
                            mView.showCodeText("重新获取(" + aLong + "s)");
                        }
                    }
                });
        addSubscribe(intervalSubscription);
    }

    @Override
    public void stopInterval() {

        if (intervalSubscription != null && !intervalSubscription.isDisposed()) {
            intervalSubscription.dispose();
        }

    }

}
